import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslint from 'vite-eslint-plugin'
import removeConsole from 'vite-plugin-remove-console'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vueI18n from '@intlify/vite-plugin-vue-i18n'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
// https://vitejs.dev/config/

export default defineConfig(({ mode /* , command */ }) => {
  const env = loadEnv(mode, process.cwd())

  const prodPlugins = []
  if (env.VITE_BUILD_TYPE === 'prod') {
    prodPlugins.push(removeConsole())
  }
  return {
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        neuron: resolve(__dirname, 'src/neuron-common'),
      },
    },
    plugins: [
      vue(),
      eslint(),

      vueI18n({
        include: resolve(__dirname, 'src/config/languages/locales/**'),
      }),

      AutoImport({
        // targets to transform
        include: [
          /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
          /\.vue$/,
          /\.vue\?vue/, // .vue
          /\.md$/, // .md
        ],

        // global imports to register
        imports: [
          // presets
          'vue',
          'vue-i18n',
          'vue-router',
          // custom
          {
            '@vueuse/core': [
              // named imports
              'useMouse', // import { useMouse } from '@vueuse/core',
              // alias
              ['useFetch', 'useMyFetch'], // import { useFetch as useMyFetch } from '@vueuse/core',
            ],
            axios: [
              // default imports
              ['default', 'axios'], // import { default as axios } from 'axios',
            ],
            // '[package-name]': [
            //   '[import-names]',
            //   // alias
            //   ['[from]', '[alias]'],
            // ],
          },
        ],
        dts: './src/types/auto-imports.d.ts',
        resolvers: [
          ElementPlusResolver(), // Auto import icon components
          // 自动导入图标组件
          IconsResolver({
            prefix: 'Icon',
          }),
        ],
        eslintrc: {
          enabled: true, // <-- this
        },
      }),
      Components({
        dts: './src/types/components.d.ts',
        resolvers: [
          // Auto register icon components
          // 自动注册图标组件
          IconsResolver({
            enabledCollections: ['ep'],
          }),
          // Auto register Element Plus components
          // 自动导入 Element Plus 组件
          ElementPlusResolver(),
        ],
      }),

      Icons({
        autoInstall: true,
      }),

      ...prodPlugins,
    ],
    server: {
      headers: {
        'Access-Control-Allow-Origin': '*',
      },
      proxy: {
        '/apollo/': {
          target: 'https://dev.neuroncloud.ai',
          changeOrigin: true,
        },
        '/gateway/': {
          target: 'https://dev.neuroncloud.ai',
          changeOrigin: true,
        },
        '/gatewayka/': {
          target: 'https://dev.neuroncloud.ai',
          changeOrigin: true,
        },
        '/login/': {
          target: 'https://login.dev.neuroncloud.ai',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/login/, ''),
        },
        '/neuron-bi-ui/': {
          target: 'https://bi-ui.dev.neuroncloud.ai',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/neuron-bi-ui/, ''),
        },
        '/neuron-dashboard-builder-ui/': {
          target: 'https://dashboard-builder-ui.dev.neuroncloud.ai',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/neuron-dashboard-builder-ui/, ''),
        },
        '/neuron-ams-ui/': {
          target: 'https://ams-ui.dev.neuroncloud.ai',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/neuron-ams-ui/, ''),
        },
        '/neuron-task-am-ui/': {
          target: 'https://task.dev.neuroncloud.ai/',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/neuron-task-am-ui/, ''),
        },
        '/model-am-ui/': {
          target: 'https://model-am.dev.neuroncloud.ai',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/model-am-ui/, ''),
        },
      },
    },
  }
})
